/**
 * mainwindow.cpp is part of Gedule.
 *
 * Gedule is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gedule is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gedule.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QAction>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QCloseEvent>
#include <QProgressBar>
#include "utils/logging/logmanager.h"
#include "gedulemgr.h"

// Icons:
const char * MainWindow::OPEN_ICON      = ":/res/Folder.png";
const char * MainWindow::SAVE_ICON      = ":/res/Save.png";
const char * MainWindow::SAVE_AS_ICON   = ":/res/Save_as.png";
const char * MainWindow::EXIT_ICON      = ":/res/exit.png";
const char * MainWindow::ABOUT_ICON     = ":/res/Info.png";
const char * MainWindow::MODEL_ICON     = ":/res/model.png";
const char * MainWindow::SCHEDULE_ICON  = ":/res/timespan.png";

// Action's text:
const char * MainWindow::OPEN_ACTION_TEXT       = "&Open";
const char * MainWindow::OPEN_ACTION_TOOLTIP    = "Open a model";
const char * MainWindow::SAVE_ACTION_TEXT       = "&Save";
const char * MainWindow::SAVE_ACTION_TOOLTIP    = "Save a model";
const char * MainWindow::SAVE_AS_ACTION_TEXT    = "&Save as...";
const char * MainWindow::SAVE_AS_ACTION_TOOLTIP = "Save a model as...";
const char * MainWindow::EXIT_ACTION_TEXT       = "&Exit";
const char * MainWindow::EXIT_ACTION_TOOLTIP    = "Exit from Gedule";
const char * MainWindow::ABOUT_ACTION_TEXT      = "&About";
const char * MainWindow::ABOUT_ACTION_TOOLTIP   = "About Gedule";

// Menu entries:
const char * MainWindow::FILE_MENU  = "&File";
const char * MainWindow::HELP_MENU  = "&Help";

// Toolbars:
const char * MainWindow::MAIN_TOOLBAR = "MainToolbar";

// Titles:
const char * MainWindow::WND_TITLE          = "Gedule";
const char * MainWindow::ABOUT_DLG_TITLE    = "About Gedule";
const char * MainWindow::MODEL_TAB_TITLE    = "Model";
const char * MainWindow::SCHEDULE_TAB_TITLE = "Schedule";

///////////////////////////////////////////////////////////////
MainWindow::MainWindow( QWidget * a_parent) :
    QMainWindow( a_parent ),
    m_currentFile( "" ),
    m_centralWidget( 0 ),
    m_fileMenu( 0 ),
    m_helpMenu( 0 ),
    m_mainToolBar( 0 ),
    //m_newModelAction( 0 ),
    m_openAction( 0 ),
    m_saveAction( 0 ),
    m_saveAsAction( 0 ),
    m_exitAction( 0 ),
    m_aboutAction( 0 )
{
    setIconSize( QSize( 16, 16 ) );
    setWindowIcon( QIcon( ) );
    setWindowTitle( WND_TITLE );

    CreateCentralWidget();
    CreateActions();
    CreateMenus();
    CreateToolBars();
    CreateStatusBar();
    ReadSettings();

    MapSignalSlots();

    showMaximized();
}

///////////////////////////////////////////////////////////////
MainWindow::~MainWindow()
{
}

///////////////////////////////////////////////////////////////
void MainWindow::MapSignalSlots()
{
}

///////////////////////////////////////////////////////////////
void MainWindow::CreateCentralWidget()
{
    m_centralWidget = new QTabWidget( this );
    m_centralWidget->addTab( new QWidget(), QIcon( MODEL_ICON ),    tr( MODEL_TAB_TITLE ) );
    m_centralWidget->addTab( new QWidget(), QIcon( SCHEDULE_ICON ), tr( SCHEDULE_TAB_TITLE ) );

    setCentralWidget( m_centralWidget );
}

///////////////////////////////////////////////////////////////
void MainWindow::CreateActions()
{
    m_openAction = new QAction( QIcon( OPEN_ICON ), tr( OPEN_ACTION_TEXT ), this );
    m_openAction->setShortcuts( QKeySequence::Open );
    m_openAction->setStatusTip( tr( OPEN_ACTION_TOOLTIP ) );
    connect( m_openAction, SIGNAL( triggered() ), this, SLOT( Open() ) );

    m_saveAction = new QAction( QIcon( SAVE_ICON ), tr( SAVE_ACTION_TEXT ), this );
    m_saveAction->setShortcuts( QKeySequence::Save );
    m_saveAction->setStatusTip( tr( SAVE_ACTION_TOOLTIP ) );
    m_saveAction->setEnabled( false );
    connect( m_saveAction, SIGNAL( triggered() ), this, SLOT( Save() ) );

    m_saveAsAction = new QAction( QIcon( SAVE_AS_ICON ), tr( SAVE_AS_ACTION_TEXT ), this );
    m_saveAsAction->setShortcuts( QKeySequence::SaveAs );
    m_saveAsAction->setStatusTip( tr( SAVE_AS_ACTION_TOOLTIP ) );
    connect( m_saveAsAction, SIGNAL( triggered() ), this, SLOT( SaveAs() ) );

    m_exitAction = new QAction( QIcon( EXIT_ICON ), tr( EXIT_ACTION_TEXT ), this );
    m_exitAction->setShortcuts( QKeySequence::Close );
    m_exitAction->setStatusTip( tr( EXIT_ACTION_TOOLTIP ) );
    connect( m_exitAction, SIGNAL( triggered() ), this, SLOT( close() ) );

    m_aboutAction = new QAction( QIcon( ABOUT_ICON ), tr( ABOUT_ACTION_TEXT ), this );
    m_aboutAction->setStatusTip( tr( ABOUT_ACTION_TOOLTIP ) );
    connect( m_aboutAction, SIGNAL( triggered() ), this, SLOT( About() ) );
}

///////////////////////////////////////////////////////////////
void MainWindow::CreateMenus()
{
    m_fileMenu = menuBar()->addMenu( tr( FILE_MENU ) );
    //m_fileMenu->addAction(newAct);
    m_fileMenu->addAction( m_openAction );
    m_fileMenu->addAction( m_saveAction );
    m_fileMenu->addAction( m_saveAsAction );
    m_fileMenu->addSeparator();
    m_fileMenu->addAction( m_exitAction );

    menuBar()->addSeparator();

    m_helpMenu = menuBar()->addMenu( tr( HELP_MENU ) );
    m_helpMenu->addAction( m_aboutAction );
}

///////////////////////////////////////////////////////////////
void MainWindow::CreateToolBars()
{
    m_mainToolBar = addToolBar( tr( MAIN_TOOLBAR ) );
    m_mainToolBar->addAction( m_openAction );
    m_mainToolBar->addAction( m_saveAction );
    m_mainToolBar->addAction( m_saveAsAction );
}

///////////////////////////////////////////////////////////////
void MainWindow::CreateStatusBar()
{
    statusBar()->showMessage( tr( "Ready" ) );
}

///////////////////////////////////////////////////////////////
void MainWindow::ReadSettings()
{

}

///////////////////////////////////////////////////////////////
void MainWindow::WriteSettings()
{

}

///////////////////////////////////////////////////////////////
void MainWindow::CloseEvent( QCloseEvent * a_event )
{
    if ( true ) // check for changes
    {
        WriteSettings();
        a_event->accept();
    }
    else
        a_event->ignore();
}

///////////////////////////////////////////////////////////////
void MainWindow::Open()
{
    if( true ) // check for changes
    {
        QString fileName = QFileDialog::getOpenFileName( this );
        if( !fileName.isEmpty() )
        {
            m_currentFile = fileName.toStdString();
            if( !GET_GEDULE_MGR().OpenModel( m_currentFile ) )
                QMessageBox::critical( this, tr( "Error" ), tr( "Could not open file" ) );
        }
    }
}

///////////////////////////////////////////////////////////////
void MainWindow::Save()
{
    if( !GET_GEDULE_MGR().SaveModel( m_currentFile ) )
        QMessageBox::critical( this, tr( "Error" ), tr( "Could not open file" ) );
}

///////////////////////////////////////////////////////////////
void MainWindow::SaveAs()
{
    QString fileName = QFileDialog::getSaveFileName( this );
    if( !fileName.isEmpty() )
    {
        m_currentFile = fileName.toStdString();
        if( !GET_GEDULE_MGR().SaveModel( m_currentFile ) )
            QMessageBox::critical( this, tr( "Error" ), tr( "Could not open file" ) );
    }
}

///////////////////////////////////////////////////////////////
void MainWindow::About()
{
    QMessageBox::about(this, tr( ABOUT_DLG_TITLE ), tr( "<b>Gedule</b> ..." ) );
}
